package com.loan.web.pdf;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.loan.common.pdf.PDFUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;

@Slf4j
public class TestPdf {

    // jh明细生成函数
    public static void mainJhDetail(String[] args) throws Exception {
        // 生成的PDF文件地址
        log.info("参数以处理完成，开始生成PDF文件信息...");
        OutputStream outputStream = null;
        try {
            // 定义全局的字体静态变量
            BaseFont bf;

            File file = new File("E:\\HzdftxDocuments\\TempInfo\\jhtestpdf.pdf");
            outputStream = new FileOutputStream(file);

            String fontPath = "C:\\Windows\\Fonts\\Dengl.ttf";
            // 最大宽度
            //创建字体
            bf = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            //使用字体并给出颜色
            Font font = new Font(bf, 16, 1, BaseColor.BLACK);
            // 不同字体（这里定义为同一种字体：包含不同字号、不同style）
            BaseFont bfChinese = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            Font cellContentFont = new Font(bfChinese, 8, 1);

            // 设置PDF大小
            Document document = new Document(new RectangleReadOnly(842f, 595f)); // A4纸张
            //设置页边距  60:左边距，60：右边距，72：上边距，72：下边距
            document.setMargins(30, 30, 40, 40);
            PdfWriter writer = PdfWriter.getInstance(document, outputStream);
            //添加页码
            // writer.setPageEvent(new PdfPageUtil());
            //打开生成的pdf文件
            document.open();
            Integer allTotal = 360;
            Integer pNum = 171;
            int m = allTotal % pNum;
            int a = allTotal / pNum;
            Integer pageNum = m > 0 ? (a + 1) : a;
            for (int num = 0; num < pageNum; num++) {
                if (num > 0) {
                    // 新增一页
                    document.newPage();
                }
                //设置内容
                Paragraph paragraph = new Paragraph("中国建设银行个人活期账户全部交易明细", font);
                paragraph.setAlignment(1); // 居中显示
                //引用内容
                document.add(paragraph);

                PdfPTable tb = new PdfPTable(Math.toIntExact(4));
                tb.setSpacingBefore(25f);
                tb.setWidthPercentage(100);
                PDFUtil.getCellValue(tb, "卡号/账号:6236682430011671219", cellContentFont, null, 0);
                PDFUtil.getCellValue(tb, "客户名称：燃源晨", cellContentFont, null, 0);
                PDFUtil.getCellValue(tb, "起始日期：20230706", cellContentFont, null, 0);
                PDFUtil.getCellValue(tb, "结束日期：20240706", cellContentFont, null, 0);
                document.add(tb);
                // 设置表格
                float[] columnWidths = new float[]{25.0f, 109.7f, 58.2f, 40.0f, 58.2f, 75.2f, 76.0f, 143.3f, 197.0f};
                PdfPTable table1 = new PdfPTable(columnWidths);
                table1.setSpacingBefore(8f);
                // 设置表格宽度为100%
                table1.setWidthPercentage(100.0F);
                table1.getDefaultCell().setHorizontalAlignment(1);
                log.info("设置表格表头标题信息...");
                PDFUtil.getCellTitleValue(table1, "序号", cellContentFont, 1, 0, 29);
                PDFUtil.getCellTitleValue(table1, "摘要", cellContentFont, 1, 0, 29);
                PDFUtil.getCellTitleValue(table1, "币别", cellContentFont, 1, 0, 29);
                PDFUtil.getCellTitleValue(table1, "钞汇", cellContentFont, 1, 0, 29);
                PDFUtil.getCellTitleValue(table1, "交易日期", cellContentFont, 1, 0, 29);
                PDFUtil.getCellTitleValue(table1, "交易金额", cellContentFont, 1, 0, 29);
                PDFUtil.getCellTitleValue(table1, "账户余额", cellContentFont, 1, 0, 29);
                PDFUtil.getCellTitleValue(table1, "交易地点/附言", cellContentFont, 1, 0, 29);
                PDFUtil.getCellTitleValue(table1, "对方账号与户名", cellContentFont, 1, 0, 29);
                document.add(table1);

                // 设置表格内容
                PdfPTable table2 = new PdfPTable(columnWidths);
                // 设置表格宽度为100%
                table2.setWidthPercentage(100.0F);
                table2.getDefaultCell().setHorizontalAlignment(1);
                int flag = 0;
                if (num == (pageNum - 1)) {
                    for (int i = 0; i < pNum; i++) {
                        flag = (i % 9) + 1;
                        int alignFlag = 1;
                        if (flag == 1 || flag == 2 || flag == 8 || flag == 9) {
                            alignFlag = 0;
                        } else if (flag == 6 || flag == 7){
                            alignFlag = 2;
                        } else {
                            alignFlag = 1;
                        }
                        if (i >= m) {
                            if (i >= 162) {
                                PDFUtil.getCellTitleValue(table2, "", cellContentFont, alignFlag, 1, 20);
                            } else {
                                PDFUtil.getCellTitleValue(table2, "", cellContentFont, alignFlag, 3, 20);
                            }
                        } else {
                            if (i >= 162) {
                                if (flag == 1) {
                                    PDFUtil.getCellTitleValue(table2, "647", cellContentFont, alignFlag, 1, 20);
                                } else if (flag == 2) {
                                    PDFUtil.getCellTitleValue(table2, "无卡自助消费", cellContentFont, alignFlag, 1, 20);
                                } else if (flag == 3) {
                                    PDFUtil.getCellTitleValue(table2, "人民币元", cellContentFont, alignFlag, 1, 20);
                                } else if (flag == 4){
                                    PDFUtil.getCellTitleValue(table2, "钞", cellContentFont, alignFlag, 1, 20);
                                } else if (flag == 5){
                                    PDFUtil.getCellTitleValue(table2, "20240701", cellContentFont, alignFlag, 1, 20);
                                } else if (flag == 6){
                                    PDFUtil.getCellTitleValue(table2, "-6,194.00", cellContentFont, alignFlag, 1, 20);
                                } else if (flag == 7){
                                    PDFUtil.getCellTitleValue(table2, "113,902.66", cellContentFont, alignFlag, 1, 20);
                                } else if (flag == 8){
                                    PDFUtil.getCellTitleValue(table2, "***", cellContentFont, alignFlag, 1, 20);
                                } else {
                                    PDFUtil.getCellTitleValue(table2, "029100330000*****0000/待***户款项410672823", cellContentFont, alignFlag, 1, 20);
                                }
                            } else {
                                if (flag == 1) {
                                    PDFUtil.getCellTitleValue(table2, "647", cellContentFont, alignFlag, 3, 20);
                                } else if (flag == 2) {
                                    PDFUtil.getCellTitleValue(table2, "无卡自助消费", cellContentFont, alignFlag, 3, 20);
                                } else if (flag == 3) {
                                    PDFUtil.getCellTitleValue(table2, "人民币元", cellContentFont, alignFlag, 3, 20);
                                } else if (flag == 4){
                                    PDFUtil.getCellTitleValue(table2, "钞", cellContentFont, alignFlag, 3, 20);
                                } else if (flag == 5){
                                    PDFUtil.getCellTitleValue(table2, "20240701", cellContentFont, alignFlag, 3, 20);
                                } else if (flag == 6){
                                    PDFUtil.getCellTitleValue(table2, "-6,194.00", cellContentFont, alignFlag, 3, 20);
                                } else if (flag == 7){
                                    PDFUtil.getCellTitleValue(table2, "113,902.66", cellContentFont, alignFlag, 3, 20);
                                } else if (flag == 8){
                                    PDFUtil.getCellTitleValue(table2, "***", cellContentFont, alignFlag, 3, 20);
                                } else {
                                    PDFUtil.getCellTitleValue(table2, "029100330000*****0000/待***户款项410672823", cellContentFont, alignFlag, 3, 20);
                                }
                            }
                        }
                    }
                } else {
                    for (int i = 0; i <= pNum; i++) {
                        flag = (i % 9) + 1;
                        int alignFlag = 1;
                        if (flag == 1 || flag == 2 || flag == 8 || flag == 9) {
                            alignFlag = 0;
                        } else if (flag == 6 || flag == 7){
                            alignFlag = 2;
                        } else {
                            alignFlag = 1;
                        }
                        if (i >= 162) {
                            if (flag == 1) {
                                PDFUtil.getCellTitleValue(table2, "647", cellContentFont, alignFlag, 1, 20);
                            } else if (flag == 2) {
                                PDFUtil.getCellTitleValue(table2, "无卡自助消费", cellContentFont, alignFlag, 1, 20);
                            } else if (flag == 3) {
                                PDFUtil.getCellTitleValue(table2, "人民币元", cellContentFont, alignFlag, 1, 20);
                            } else if (flag == 4){
                                PDFUtil.getCellTitleValue(table2, "钞", cellContentFont, alignFlag, 1, 20);
                            } else if (flag == 5){
                                PDFUtil.getCellTitleValue(table2, "20240701", cellContentFont, alignFlag, 1, 20);
                            } else if (flag == 6){
                                PDFUtil.getCellTitleValue(table2, "-6,194.00", cellContentFont, alignFlag, 1, 20);
                            } else if (flag == 7){
                                PDFUtil.getCellTitleValue(table2, "113,902.66", cellContentFont, alignFlag, 1, 20);
                            } else if (flag == 8){
                                PDFUtil.getCellTitleValue(table2, "***", cellContentFont, alignFlag, 1, 20);
                            } else {
                                PDFUtil.getCellTitleValue(table2, "029100330000*****0000/待***户款项410672823", cellContentFont, alignFlag, 1, 20);
                            }
                        } else {
                            if (flag == 1) {
                                PDFUtil.getCellTitleValue(table2, "647", cellContentFont, alignFlag, 3, 20);
                            } else if (flag == 2) {
                                PDFUtil.getCellTitleValue(table2, "无卡自助消费", cellContentFont, alignFlag, 3, 20);
                            } else if (flag == 3) {
                                PDFUtil.getCellTitleValue(table2, "人民币元", cellContentFont, alignFlag, 3, 20);
                            } else if (flag == 4){
                                PDFUtil.getCellTitleValue(table2, "钞", cellContentFont, alignFlag, 3, 20);
                            } else if (flag == 5){
                                PDFUtil.getCellTitleValue(table2, "20240701", cellContentFont, alignFlag, 3, 20);
                            } else if (flag == 6){
                                PDFUtil.getCellTitleValue(table2, "-6,194.00", cellContentFont, alignFlag, 3, 20);
                            } else if (flag == 7){
                                PDFUtil.getCellTitleValue(table2, "113,902.66", cellContentFont, alignFlag, 3, 20);
                            } else if (flag == 8){
                                PDFUtil.getCellTitleValue(table2, "***", cellContentFont, alignFlag, 3, 20);
                            } else {
                                PDFUtil.getCellTitleValue(table2, "029100330000*****0000/待***户款项410672823", cellContentFont, alignFlag, 3, 20);
                            }
                        }
                    }
                }
                document.add(table2);
                // 页码
                PdfPTable tb1 = new PdfPTable(Math.toIntExact(4));
                tb1.setWidthPercentage(100);
                tb1.setSpacingBefore(7f);
                PDFUtil.getCellValue(tb1, "生成时间:2024-07-06 16:24:16", cellContentFont, null, 0);
                PDFUtil.getCellValue(tb1, "", cellContentFont, null, 0);
                PDFUtil.getCellValue(tb1, "当前页：" + (num + 1), cellContentFont, null, 0);
                PDFUtil.getCellValue(tb1, "总页数：36", cellContentFont, null, 0);
                document.add(tb1);

                log.info("添加图片logo到PDF文件中...");
                // 添加图片到PDF文件中
                PDFUtil.addImg(document, "E:\\HzdftxDocuments\\TempInfo\\jh_logo.png", 110f, 110f, 70f, 110f);
            }
            //关闭文档
            document.close();

        } catch (DocumentException | FileNotFoundException e) {
            log.error("生成PDF文件失败: {}", e.getMessage());
            log.error(e.getMessage(), e);
        } finally {
            if (outputStream != null) {
                outputStream.close();
            }
        }
    }
}
